package mkv.MyGUI;
import processing.core.*;

/**
 * MyGUILabel is just a one line text thingy. Its advantages over drawing the text yourself include
 * the fact it loads the font and colours already set in the MyGUIStyle object, and will be scaled/rotated
 * appropriately based no the parent group.
 * 
 * @author John Beech
 */

public class MyGUILabel extends MyGUIObject {
    
    public String _text;
    public int alignment; 
    
    /**
     * The default MyGUILabel constructor takes an x, y coordinate, the text you want to
     * include and an obligatory reference to the root applet, so it knows where to draw.
     * 
     * @param root 
     * @param x
     * @param y
     * @param label
     */
    public MyGUILabel(PApplet root, String label, int x, int y) {
        super(root, x, y);
        _text = label;
        alignment = PApplet.LEFT;
    }
    
    public MyGUILabel(PApplet root, String label, int x, int y, int width, int height) {
        super(root, x, y);
        _text = label;
        _width = width;
        _height = height;
        alignment = PApplet.LEFT;
    }
    
    /**
     * Allows the alignment to be set, usual values are LEFT, CENTER and RIGHT.
     * @param align expects PApplet.LEFT, PApplet.CENTER and PApplet.RIGHT.
     */
    public void align(int align) {
        alignment = align;
    }
    
    public void setAlignment(int align) {
        alignment = align;
    }
    
    public void setText(String text) {
        _text = text; 
    }
    
    public void drawStates() {
        // alias to shorten _style property to st
        MyGUIStyle st = _style;
        if(_style == null) {
          st = _parent.getStyle();
        } else {
          st = _style;
        }
        // set text offset (used each time text is drawn to screen)
        int textVOffset = st.fontSize/2 - st.padding/2 - 1;
        int textHOffset = 0;
        // move draw object to coordinates
        _root.pushMatrix();
        _root.translate(_x, _y);
        _root.scale(_scale);
        _root.rotate(PApplet.radians(_rotation));
        // Set default properties
        _root.textFont(st.font, st.fontSize);
        _root.textAlign(alignment);
        _root.fill(st.buttonText);
        // draw text
        if(_width > 0 && _height > 0) {
            _root.text(_text, 0, textVOffset, _width, _height);
        } else {
            _root.text(_text, 0, textVOffset);
        }
        // reset matrix
        _root.popMatrix();
    }
}
